package com.cskaoyan.wordmemorize.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.wordmemorize.dao.entity.ReviewTopicDO;
import com.cskaoyan.wordmemorize.dao.entity.TopicOptionDO;
import com.cskaoyan.wordmemorize.dao.entity.VocDO;
import com.cskaoyan.wordmemorize.dao.mapper.ReviewTopicMapper;
import com.cskaoyan.wordmemorize.dao.mapper.TopicOptionsMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocMapper;
import com.cskaoyan.wordmemorize.excel.SingleExcelData;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class SingleExcelDataListener implements ReadListener<SingleExcelData> {

    private ReviewTopicMapper  reviewTopicMapper;
    private TopicOptionsMapper topicOptionsMapper;

    @Getter
    public  int lines;
    private VocMapper vocMapper;

    public SingleExcelDataListener(ReviewTopicMapper reviewTopicMapper, TopicOptionsMapper topicOptionsMapper,VocMapper vocMapper) {
        this.reviewTopicMapper = reviewTopicMapper;
        this.topicOptionsMapper = topicOptionsMapper;
        this.vocMapper = vocMapper;
    }

    @Override
    public void invoke(SingleExcelData singleExcelData, AnalysisContext analysisContext) {
        String jsonString = JSON.toJSONString(singleExcelData);
        log.info("jsonString = " + jsonString);
        saveData(singleExcelData);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }



    private void saveData(SingleExcelData data) {
        log.info("开始存储数据库！");
        String topic = data.getTopic();
        List<String> options = new ArrayList<>();
        options.add(data.getOptionA());
        options.add(data.getOptionB());
        options.add(data.getOptionC());
        options.add(data.getOptionD());
        String answer = options.get(Integer.parseInt(data.getAnswer())-1);
        //插入review_topic表
        ReviewTopicDO reviewTopicDO = new ReviewTopicDO();
        reviewTopicDO.setTopic(topic);
        reviewTopicDO.setTopicType(1);
        reviewTopicDO.setVocabulary(answer);
        QueryWrapper<VocDO> vocDOQueryWrapper = new QueryWrapper<>();
        vocDOQueryWrapper.eq("content", answer);
        VocDO vocDO = vocMapper.selectOne(vocDOQueryWrapper);
        reviewTopicDO.setVocId(vocDO.getId());
        reviewTopicMapper.insert(reviewTopicDO);
        //插入topic_option表
        Long tid = reviewTopicDO.getId();
        int index = 0;
        for(String option : options) {
            TopicOptionDO topicOptionDO = new TopicOptionDO();
            topicOptionDO.setTid(tid);
            topicOptionDO.setItem(option);
            if(option.equals(answer)) {
                topicOptionDO.setIsAnswer(1);
            }
            else topicOptionDO.setIsAnswer(0);
            topicOptionDO.setOptionNumber(++index);
            topicOptionsMapper.insert(topicOptionDO);
        }
        log.info("存储数据库成功！");
        lines++;

    }
}
